﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using PloobsEngine.Modelo;

namespace PloobsEngine.Material
{    
    /// <summary>
    /// Spherical Bilboard
    /// </summary>
    public class SphericalBilboardDeferred : IShader
    {
        public SphericalBilboardDeferred()
        {
            Initialize();
        }
        
        private Effect _shader;
        private Vector2 scale = new Vector2(100, 100);

        /// <summary>
        /// Default Vector2(100, 100);
        /// </summary>
        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        private Vector4 atenuation = Vector4.One;

        /// <summary>
        /// Default Vector4.One
        /// </summary>
        public Vector4 Atenuation
        {
            get { return atenuation; }
            set { atenuation = value; }
        }        

        public override MaterialType MaterialType
        {
            get { return MaterialType.DEFERRED; }
        }

        
        public override void  Draw(IModelo modelo, PloobsEngine.SceneControl.IRenderHelper render, PloobsEngine.Cameras.ICamera cam)
        {

            EngineStuff.GraphicsDevice.RenderState.CullMode = CullMode.None; 

            Vector3 dir = cam.Target - cam.Position;
            dir.Normalize();
            _shader.Parameters["forward"].SetValue(dir);
            _shader.Parameters["camUp"].SetValue(cam.Up);
            _shader.Parameters["scaleX"].SetValue(scale.X);
            _shader.Parameters["scaleY"].SetValue(scale.Y);
            _shader.Parameters["xWorld"].SetValue(Matrix.Identity);
            _shader.Parameters["xView"].SetValue(cam.View);
            _shader.Parameters["xProjection"].SetValue(cam.Projection);                        
            _shader.Parameters["xBillboardTexture"].SetValue(modelo.getTexture(TextureType.DIFFUSE));
            _shader.Parameters["atenuation"].SetValue(atenuation);           
                       

            _shader.Begin();
            BatchInformation batchInfo = modelo.GetBatchInformation(0)[0];
            EngineStuff.GraphicsDevice.Vertices[0].SetSource(modelo.GetVertexBuffer(0), batchInfo.StreamOffset, batchInfo.VertexStride);
            EngineStuff.GraphicsDevice.VertexDeclaration = batchInfo.VertexDeclaration;
            {               
                    _shader.Parameters["applyLight"].SetValue(true);
                    EngineStuff.GraphicsDevice.RenderState.AlphaTestEnable = true;
                    EngineStuff.GraphicsDevice.RenderState.AlphaFunction = CompareFunction.GreaterEqual;
                    EngineStuff.GraphicsDevice.RenderState.ReferenceAlpha = 200;

                    _shader.CurrentTechnique.Passes[0].Begin();
                    EngineStuff.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, batchInfo.StartVertex, batchInfo.PrimitiveCount);
                    _shader.CurrentTechnique.Passes[0].End();

                    //_shader.Parameters["applyLight"].SetValue(false);
                    //EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
                    //EngineStuff.GraphicsDevice.RenderState.AlphaBlendEnable = true;
                    //EngineStuff.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
                    //EngineStuff.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;

                    //EngineStuff.GraphicsDevice.RenderState.AlphaTestEnable = true;
                    //EngineStuff.GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Less;
                    //EngineStuff.GraphicsDevice.RenderState.ReferenceAlpha = 200;

                    //_shader.CurrentTechnique.Passes[0].Begin();
                    //EngineStuff.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, batchInfo.StartVertex, batchInfo.PrimitiveCount);
                    //_shader.CurrentTechnique.Passes[0].End();
            

            }               
            
            
            _shader.End();

            EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
            EngineStuff.GraphicsDevice.RenderState.AlphaTestEnable = false;  
            EngineStuff.GraphicsDevice.RenderState.AlphaBlendEnable = false;
            EngineStuff.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
        }


        public override void  Initialize()
        {
            this._shader = EngineStuff.InternalContentManager.GetAsset<Effect>("SphericalBillboard");            
        }


    


    }
}
